package servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Enumeration;

@WebServlet(name = "Demo7_SessionLoginServlet", value = "/sessionLogin")
public class Demo7_SessionLoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        this.doPost(request, response);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String username = "";
        String password = "";

        //1.获取当前客户端的session对象。Cookie中的sessionID已经存储到了request中，所以自动获取客户端的session对象
        HttpSession session = request.getSession();


        //1.1 获取session中的所有信息名
        System.out.println("=====names=====");
        Enumeration<String> attributeNames = session.getAttributeNames();
        while(attributeNames.hasMoreElements()){
            System.out.println(attributeNames.nextElement());
        }

        //1,2 判断当前的session是否为一个新的session，第一次访问此地址一定是新session
        System.out.println("isNew："+session.isNew());

        //1.3 获取当前session的sessionId
        System.out.println("sessionId:"+session.getId());

        //1.4 获取当前session的创建时间
        System.out.println("createTime:"+new Date(session.getCreationTime()));

        //1.5 获取当前session的最后一次访问的时间
        System.out.println("lastAccessedTime:"+new Date(session.getLastAccessedTime()));

        //1.6 获取当前session的最大时间间隔
        System.out.println("时间间隔"+session.getMaxInactiveInterval());

        //1.7 如果默认的30分钟不符合当前逻辑，可以进行修改
        session.setMaxInactiveInterval(3600);


        //2.通过客户端的sessionID 从服务端中的session获取用户名和密码
        username = (String) session.getAttribute("username");
        password = (String) session.getAttribute("password");

        System.out.println(username);
        System.out.println(password);

        //3.如果session没有用户名和密码数据或者 sessionId 与 session 对应不上
        if(null==username || username.equals("")){
            //如果session中没有username，从页面的文本框读username
            username = request.getParameter("username");
        }

        if(null==password || password.equals("")){
            //如果session中没有password，从页面的文本框读password
            password = request.getParameter("password");
        }

        System.out.println(username);
        System.out.println(password);

        //如果登录成功的话
        if(username.equals("tiger") && password.equals("scott")){

            //4.如果登录成功，把用户名和密码设置到session中
            //TODO 此处每次登录都需要重新设置session信息，属于冗余的设置
            session.setAttribute("username",username);
            session.setAttribute("password",password);

            response.setContentType("text/html;charset=utf-8");
            PrintWriter writer = response.getWriter();
            writer.print("登录成功");
            writer.close();
        }else{
            response.sendRedirect("/html/login.html");
        }

    }
}
